package com.joe.learn.dubbo.callback.consumer;

import com.joe.learn.dubbo.callback.api.CallbackListener;
import com.joe.learn.dubbo.callback.api.CallbackService;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.Date;

public class Consumer {

    /**
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
                new String[]{"classpath:consumer.xml"});
        context.start();

        // 参数回调
        CallbackService callbackService = (CallbackService) context.getBean("callbackService");

        for (int i = 0; i < 10; i++) {
            sendKey(callbackService, i + "");
        }

    }

    private static void sendKey(CallbackService callbackService, final String key) throws InterruptedException {
        final boolean[] b = {true};
        String result = callbackService.addListener(key, new CallbackListener() {
            @Override
            public void changed(String msg) {
                b[0] = !msg.contains(key);
            }
        });
        String dateStr = DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss");
        while (b[0]) {
            Thread.sleep(30000);
        }
        System.out.println(dateStr + " :result=" + result);
    }
}